const nodemailer = require('nodemailer');

// 创建邮件传输器
const createTransporter = () => {
  return nodemailer.createTransport({
    host: process.env.EMAIL_HOST || 'smtp.qq.com',
    port: process.env.EMAIL_PORT || 587,
    secure: false,
    auth: {
      user: process.env.EMAIL_USER,
      pass: process.env.EMAIL_PASS
    }
  });
};

// 发送验证码邮件
const sendVerificationEmail = async (email, code, type) => {
  try {
    const transporter = createTransporter();
    
    let subject, html;
    
    switch (type) {
      case 'register':
        subject = 'LinkMate - 邮箱验证码';
        html = `
          <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
            <h2 style="color: #1890ff;">欢迎加入 LinkMate！</h2>
            <p>您的验证码是：</p>
            <div style="background-color: #f0f0f0; padding: 20px; text-align: center; font-size: 24px; font-weight: bold; color: #1890ff; margin: 20px 0;">
              ${code}
            </div>
            <p>验证码有效期为5分钟，请及时使用。</p>
            <p>如果这不是您的操作，请忽略此邮件。</p>
            <hr style="margin: 30px 0;">
            <p style="color: #666; font-size: 12px;">此邮件由系统自动发送，请勿回复。</p>
          </div>
        `;
        break;
        
      case 'login':
        subject = 'LinkMate - 登录验证码';
        html = `
          <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
            <h2 style="color: #1890ff;">LinkMate 登录验证</h2>
            <p>您正在使用邮箱验证码登录，验证码是：</p>
            <div style="background-color: #f0f0f0; padding: 20px; text-align: center; font-size: 24px; font-weight: bold; color: #1890ff; margin: 20px 0;">
              ${code}
            </div>
            <p>验证码有效期为5分钟，请及时使用。</p>
            <p>如果这不是您的操作，请立即修改密码。</p>
            <hr style="margin: 30px 0;">
            <p style="color: #666; font-size: 12px;">此邮件由系统自动发送，请勿回复。</p>
          </div>
        `;
        break;
        
      case 'reset_password':
        subject = 'LinkMate - 密码重置验证码';
        html = `
          <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
            <h2 style="color: #1890ff;">LinkMate 密码重置</h2>
            <p>您正在重置密码，验证码是：</p>
            <div style="background-color: #f0f0f0; padding: 20px; text-align: center; font-size: 24px; font-weight: bold; color: #1890ff; margin: 20px 0;">
              ${code}
            </div>
            <p>验证码有效期为5分钟，请及时使用。</p>
            <p>如果这不是您的操作，请立即联系客服。</p>
            <hr style="margin: 30px 0;">
            <p style="color: #666; font-size: 12px;">此邮件由系统自动发送，请勿回复。</p>
          </div>
        `;
        break;
        
      default:
        throw new Error('未知的邮件类型');
    }

    const mailOptions = {
      from: process.env.EMAIL_FROM || 'LinkMate <noreply@linkmate.com>',
      to: email,
      subject: subject,
      html: html
    };

    await transporter.sendMail(mailOptions);
    console.log(`验证码邮件已发送到: ${email}`);
    
  } catch (error) {
    console.error('发送邮件失败:', error);
    throw new Error('邮件发送失败');
  }
};

// 发送通知邮件
const sendNotificationEmail = async (email, title, content) => {
  try {
    const transporter = createTransporter();
    
    const html = `
      <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
        <h2 style="color: #1890ff;">${title}</h2>
        <div style="background-color: #f9f9f9; padding: 20px; border-radius: 5px; margin: 20px 0;">
          ${content}
        </div>
        <hr style="margin: 30px 0;">
        <p style="color: #666; font-size: 12px;">此邮件由系统自动发送，请勿回复。</p>
      </div>
    `;

    const mailOptions = {
      from: process.env.EMAIL_FROM || 'LinkMate <noreply@linkmate.com>',
      to: email,
      subject: `LinkMate - ${title}`,
      html: html
    };

    await transporter.sendMail(mailOptions);
    console.log(`通知邮件已发送到: ${email}`);
    
  } catch (error) {
    console.error('发送通知邮件失败:', error);
    throw new Error('邮件发送失败');
  }
};

module.exports = {
  sendVerificationEmail,
  sendNotificationEmail
};
